import React, { Component } from 'react'

export default class Count extends Component {

  state = {
    count:0
  }

  increment = ()=>{
    const {value} = this.selectCount
    const {count} = this.state
    this.setState({
      count:Number(value)+count
    })
  }

  decrement = ()=>{
    const {value} = this.selectCount
    const {count} = this.state
    this.setState({
      count:count - Number(value)
    })
  }

  incrementIfOdd = () => {
    const {value} = this.selectCount
    const {count} = this.state
    if(count%2!==0){
      this.setState({
        count:Number(value)+count
      })
    }
    
  }

  incrementAsync = ()=>{
    const {value} = this.selectCount
    const {count} = this.state
    setTimeout(() => {
      this.setState({
        count:Number(value)+count
      })
    }, 500);
    
  }

  render() {
    return (
      <div>
        <h1>当前的值为：{this.state.count}</h1>
        <select ref={c=>this.selectCount=c}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        <button onClick={this.increment}>+</button>
        <button onClick={this.decrement}>-</button>
        <button onClick={this.incrementIfOdd}>奇数再加</button>
        <button onClick={this.incrementAsync}>异步加</button>
      </div>
    )
  }
}
